package impls.agents.dialogs;

import impls.classes.Launcher;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JDialog;

public class StationAgentDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = -4788011279060704673L;

	private Launcher owner;

	private TextField price;

	private Checkbox autoGenerate;

	private ArrayList<Integer> priceValues;

	private int stationNumbers;

	public StationAgentDialog(Launcher owner, int stationNumbers) {
		super(owner);
		this.owner = owner;
		this.stationNumbers = stationNumbers;
		this.setModal(true);
		this.setResizable(false);
		this.setLayout(new FlowLayout());
		this.setTitle("Station Agent 1");

		price = new TextField("120");

		this.getContentPane().add(new Label("Price:"));
		this.getContentPane().add(price);

		Button next = new Button("Next");
		next.addActionListener(this);

		this.getContentPane().add(next);

		Button cancel = new Button("Cancel");
		cancel.addActionListener(this);

		this.getContentPane().add(cancel);

		autoGenerate = new Checkbox("Use same settings for all Agents");
		autoGenerate.setState(true);

		this.getContentPane().add(autoGenerate);

		priceValues = new ArrayList<Integer>();

		pack();
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Cancel")) {
			owner.setAbort();
			this.dispose();
		}

		if (e.getActionCommand().equals("Next")) {
			if (autoGenerate.getState()) {
				owner.createStationsAutomatic(Integer.valueOf(price.getText()));
				this.dispose();
			} else {
				priceValues.add(Integer.valueOf(price.getText()));
				if (priceValues.size() + 1 <= stationNumbers) {
					this.setTitle("Station Agent " + (priceValues.size() + 1));
				} else {
					owner.createStations(priceValues);
					this.dispose();
				}
			}
		}
	}
}
